package com.test;


import com.example.javaproject.okhttp.TextUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cl2567 on 2016/8/4.
 */

public class PermissionConstant {//1073741875
    public static final int FLAG_PERMISSION_FLAG = 1 << 30;//1<<31被默认值占用了

    public static final int CAMERA_PERMISSION = FLAG_PERMISSION_FLAG | 1;//01
    public static final int CAMERA_MUST = FLAG_PERMISSION_FLAG | 1 << 1;//10

    public static final int LOCATION_PERMISSION = FLAG_PERMISSION_FLAG | 1 << 2;//100
    public static final int LOCATION_MUST = FLAG_PERMISSION_FLAG | 1 << 3;//1000

    public static final int MICRO_PHONE_PERMISSION = FLAG_PERMISSION_FLAG | 1 << 4;
    public static final int MICRO_PHONE_MUST = FLAG_PERMISSION_FLAG | 1 << 5;

    public static final int PHONE_PERMISSION = FLAG_PERMISSION_FLAG | 1 << 6;
    public static final int PHONE_MUST = FLAG_PERMISSION_FLAG | 1 << 7;

    public static final int STORAGE_PERMISSION = FLAG_PERMISSION_FLAG | 1 << 8;
    public static final int STORAGE_MUST = FLAG_PERMISSION_FLAG | 1 << 9;

    public static final int CONTACT_PERMISSION = FLAG_PERMISSION_FLAG | 1 << 10;
    public static final int CONTACT_MUST = FLAG_PERMISSION_FLAG | 1 << 11;


    //检查权限类型
    public static final int PERMISSION_LOCATION = 1;
    public static final int PERMISSION_CAMERA = 2;
    public static final int PERMISSION_GET_ACCOUNT = 3;


    //权限申请列表
/*    public static final String[] LOCATION_PERMISSIONS = new String[]{
            android.Manifest.permission.ACCESS_COARSE_LOCATION,
            android.Manifest.permission.ACCESS_FINE_LOCATION};

    public static final String[] CAMERA_PERMISSIONS = new String[]{
            android.Manifest.permission.CAMERA};

    //shared preference 键值，存储是否已经显示过tip
    public static final String LOCATION_TIP_SHOWN = "location_tip_shown";

    //The same as NotificationManagerCompatKitKat
    public static final String CHECK_OP_NO_THROW = "checkOpNoThrow";
    public static final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";*/


    public static String transfer2Permission(int value) {
        switch (value) {
            case PermissionConstant.CAMERA_PERMISSION:
                return "Manifest.permission.CAMERA";

            case PermissionConstant.LOCATION_PERMISSION:
                return "Manifest.permission.ACCESS_FINE_LOCATION";

            case PermissionConstant.MICRO_PHONE_PERMISSION:
                return "Manifest.permission.RECORD_AUDIO";

            case PermissionConstant.PHONE_PERMISSION:
                return "Manifest.permission.READ_PHONE_STATE";

            case PermissionConstant.STORAGE_PERMISSION:
                return "Manifest.permission.WRITE_EXTERNAL_STORAGE";

            case PermissionConstant.CONTACT_PERMISSION:
                return "Manifest.permission.WRITE_CONTACTS";
            default:
                return null;
        }
    }


    public static List<String> checkPermission(int extra) {
        ArrayList<String> list = new ArrayList<>(8);
        if (needsPermission(PermissionConstant.CAMERA_PERMISSION, extra)) {
            checkPermissionValue(list, PermissionConstant.CAMERA_PERMISSION);
        }
        if (needsPermission(PermissionConstant.LOCATION_PERMISSION, extra)) {
            checkPermissionValue(list, PermissionConstant.LOCATION_PERMISSION);
        }

        if (needsPermission(PermissionConstant.MICRO_PHONE_PERMISSION, extra)) {
            checkPermissionValue(list, PermissionConstant.MICRO_PHONE_PERMISSION);
        }

        if (needsPermission(PermissionConstant.PHONE_PERMISSION, extra)) {
            checkPermissionValue(list, PermissionConstant.PHONE_PERMISSION);
        }

        if (needsPermission(PermissionConstant.STORAGE_PERMISSION, extra)) {
            checkPermissionValue(list, PermissionConstant.STORAGE_PERMISSION);
        }

        if (needsPermission(PermissionConstant.CONTACT_PERMISSION, extra)) {
            checkPermissionValue(list, PermissionConstant.CONTACT_PERMISSION);
        }
        return list;
    }

    public static ArrayList<Boolean> getPermissionMustArray(int extra) {
        ArrayList<Boolean> list = new ArrayList<>(8);
        if (PermissionConstant.needsPermission(PermissionConstant.CAMERA_PERMISSION, extra)) {
            checkPermissionValue(list, extra, PermissionConstant.CAMERA_MUST);
        }

        if (PermissionConstant.needsPermission(PermissionConstant.LOCATION_PERMISSION, extra)) {
            checkPermissionValue(list, extra, PermissionConstant.LOCATION_MUST);
        }

        if (PermissionConstant.needsPermission(PermissionConstant.MICRO_PHONE_PERMISSION, extra)) {
            checkPermissionValue(list, extra, PermissionConstant.MICRO_PHONE_MUST);
        }

        if (PermissionConstant.needsPermission(PermissionConstant.PHONE_PERMISSION, extra)) {
            checkPermissionValue(list, extra, PermissionConstant.PHONE_MUST);
        }

        if (PermissionConstant.needsPermission(PermissionConstant.STORAGE_PERMISSION, extra)) {
            checkPermissionValue(list, extra, PermissionConstant.STORAGE_MUST);
        }

        if (PermissionConstant.needsPermission(PermissionConstant.CONTACT_PERMISSION, extra)) {
            checkPermissionValue(list, extra, PermissionConstant.CONTACT_MUST);
        }
        return list;
    }


    private static boolean needsPermission(int permissionFlag, int extra) {
        String result = transfer2Permission(permissionFlag);
        if (TextUtils.isEmpty(result)) return false;
        /*if (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(context, result)) {
            return false;
        }*/
        return (permissionFlag & extra) == permissionFlag;
    }

    private static void checkPermissionValue(ArrayList<String> list, int value) {
        String result = transfer2Permission(value);
        if (TextUtils.isEmpty(result)) return;
        /*if (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(context, result)) {
            return;
        }*/
        list.add(result);
    }

    private static void checkPermissionValue(ArrayList<Boolean> list, int extra, int value) {
        list.add((extra & value) == value);
    }
}
